package com.wn.class30.cotroller.weixin;

import com.wn.class30.entity.*;
import com.wn.class30.service.UserService;
import com.wn.class30.tools.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@Slf4j
@RequestMapping("/weixin")
@Validated
@Api(value = "用户信息相关",tags ="用户信息相关" )
public class UserController {
    //用户购买会员时，注册方法
    @Resource
    private UserService userService;
    @Resource
    private UseridUtils useridUtils;
    @Resource
    private OptionidUtils optionidUtils;
    //首页    http://yuhao.ngrok2.xiaomiqiu.cn/weixin/queryIndex
    @RequestMapping(value = "/user/register")//http://localhost:8080/weixin/user/register.html
    public void register(@Valid @NotNull String name, @Valid @NotNull String phone, @Valid @NotNull String yzm) {
        System.out.println(name);
        System.out.println(phone);
        System.out.println(yzm);
    }

    //用户注册时发送验证码
    @RequestMapping("/user/sendVercode")
    @ApiOperation(value = "用户注册时发送验证码", notes = "用户注册时发送验证码")
    public Result sendVercode(@Valid @NotNull String tel, HttpSession session) throws Exception {
        log.info("进入发送验证码方法了");
        log.info("前端输入电话:" + tel);
        //判断手机号是否在后台用户表内
        //获取随机验证码
        String vercode = SMSUtils.getVercode();
        //发送验证码
        SMSUtils.sendShortMessage(tel, vercode);
        log.info("给" + tel + "发送验证码了" + vercode);
        session.setAttribute(tel, vercode);
        Result result = new Result();
        result.setCode("200");
        result.setMassage("success");
        return result;
    }

    //校验短信验证码是否正确,正确则存入到数据库
    @RequestMapping("/user/checkVercode")
    @ApiOperation(value = "校验短信验证码", notes = "校验短信验证码")
    public Result checkVercode(@Valid @NotNull String phone, @Valid @NotNull String yzm, HttpSession session,HttpServletRequest req) throws Exception {
        Integer uid = useridUtils.queryUseridByOptenid(req);
        String userSendVercode = (String) session.getAttribute(phone);
        if (userSendVercode != null && yzm.equals(userSendVercode)) {
            //存入用户手机号码
            userService.updatePhone(uid,phone);
            log.info(yzm);
            log.info(phone);
            Result result = new Result();
            result.setCode("200");
            result.setMassage("success");
            return result;
        } else {
            Result result = new Result();
            result.setCode("500");
            result.setMassage("fiald");
            return result;
        }
    }
    @GetMapping(value = "/queryUser")
    @ApiOperation(value = "获取用户数据", notes = "获取用户数据")
    public Result queryUser(HttpServletResponse resp, HttpServletRequest req) throws Exception {
        log.info("-------------------------------------------");
        String url=req.getScheme()+"://"+ req.getServerName()+ "/weixin/queryUser";
        boolean b = optionidUtils.validateOAuthOpenId(req, resp, url);
        if (b == true) {
            String openid = (String) req.getSession().getAttribute("openid");
            List<User> users = userService.queryByOpenid(openid);
            if (users == null || users.size() == 0) {
                return new Result("200", "success", null, null);
            } else {
                req.getSession().setAttribute("me",users.get(0));
                return new Result("200", "success", users.get(0), null);
            }
        } else {
            return null;
        }
    }
    @GetMapping(value = "/queryVipName")
    @ApiOperation(value = "查询用户vip信息",notes ="查询用户vip信息" )
    public  Result queryVipName(HttpServletRequest req) throws Exception {
        Integer uid = useridUtils.queryUseridByOptenid(req);
        if(uid==null){
            return  new Result("200","success",null,null);
        }else{
            Vip vip = userService.queryLvNameByUserid(uid);
            return new Result("200","success",vip,null);
        }
    }
    @GetMapping(value = "/queryUserCupon")
    @ApiOperation(value ="查询用户优惠券",notes = "查询用户优惠券")
    public Result  queryUserCupon(HttpServletRequest req) throws  Exception{
        Integer uid = useridUtils.queryUseridByOptenid(req);
        if(uid==null){
            return  new Result("200","success",null,null);
        }else{
            List<UserCupon> userCupons = userService.queryUserCuponByUserid(uid);
            return new Result("200","success",userCupons,null);
        }

    }
    @GetMapping(value = "/user/queryMeal")
    @ApiOperation(value ="查询vip套餐",notes = "查询vip套餐")
    public Result  queryMeal() throws  Exception{
        List<Meal> meals = userService.queryMeal();
        if(meals==null || meals.size() == 0){
            return  new Result("200","success",null,null);
        }else{
            return new Result("200","success",null,meals);
        }

    }

    @GetMapping(value = "/user/getUserInfo")
    @ApiOperation(value ="获取用户信息",notes = "获取用户信息")
    public Result  getUserInfo(HttpServletRequest request) throws  Exception{
        Integer userid = new UseridUtils().queryUseridByOptenid(request);
        System.out.println("<<<<<<>>>>>>>>"+userid);
        User user = userService.queryUserByUderid(userid);
        if(user==null){
            return  new Result("200","success",null,null);
        }else{
            return new Result("200","success",user,null);
        }
    }
    @GetMapping(value = "/user/queryAllVip")
    @ApiOperation(value ="获取所有vip等级",notes = "获取所有vip等级")
    public Result  queryAllVip() throws  Exception{
        List<Vip> vipList = userService.queryAllVip();
        if(vipList==null){
            return  new Result("200","success",null,null);
        }else{
            return new Result("200","success",null,vipList);
        }
    }

    @GetMapping(value = "/user/queryUserCupon2")
    @ApiOperation(value ="查询用户和其对应的优惠券",notes = "查询用户和其对应的优惠券")
    public Result  queryUserCupon2(HttpServletRequest request) throws  Exception{
        System.out.println("查询用户和其对应的优惠券中间表");
        Integer userid = new UseridUtils().queryUseridByOptenid(request);
        List<Map<String, Object>> maps = userService.queryUserCupon2(userid);
        System.out.println(maps);
        return new Result("200","success",null,maps);

    }
    @GetMapping(value = "/user/selectliquanbao")
    @ApiOperation(value ="存储选中的优惠券id",notes = "查询用户和其对应的优惠券")
    public Result  selectliquanbao(@Valid @NotNull Integer uc_id,HttpSession session) throws  Exception{
        session.setAttribute("myselect_uc_id",uc_id);
        return new Result("200","success",null,null);
    }
    @GetMapping(value = "/user/selectliquanbao2")
    @ApiOperation(value ="订单获取选中的优惠券id",notes = "查询用户和其对应的优惠券")
    public Result  selectliquanbao2(HttpSession session) throws  Exception{
        Integer myselect_uc_id = (Integer) session.getAttribute("myselect_uc_id");
        return new Result("200","success",myselect_uc_id,null);
    }

    @GetMapping(value = "/vip/insertMealPay")
    @ApiOperation(value ="订单获取选中的优惠券id",notes = "查询用户和其对应的优惠券")
    public Result  insertMealPay(HttpServletRequest request,Integer vip_checked) throws  Exception{
        System.out.println(vip_checked);
        //查询会员套餐、积分、等级、
        //查询套餐表和vip等级表
        List<Map<String,Object>> mealAndVip = userService.selectMealAndVip(vip_checked);
        System.out.println("mealAndVip:"+mealAndVip);
        //查询用户信息
        Integer userid = useridUtils.queryUseridByOptenid(request);
        //查询用户信息
        User user = userService.queryUserByUderid(userid);
        System.out.println("user"+user);
        //新增nealpay表
        MealPay mealPay = userService.insertMealPay(mealAndVip, user);
        return new Result("200","success", mealPay.getVipOrderNum(),null);
    }

    @GetMapping(value = "/user/checkPhoneExist")
    @ApiOperation(value ="判断用户的手机号是否存在",notes = "判断用户的手机号是否存在")
    public Result  checkPhoneExist(HttpServletRequest request) throws  Exception{
        Integer uid = useridUtils.queryUseridByOptenid(request);
        if(uid==null){
            return  new Result("200","success",null,null);
        }else{
            String tel = userService.checkPhoneExist(uid);
            System.out.println("tel"+tel);
            return new Result("200","success",tel,null);
        }
    }
    @GetMapping(value = "/checkUserIsExit")
    @ApiOperation(value ="判断用户是否存在",notes = "判断用户是否存在")
    public Result  checkUserIsExit(HttpServletRequest request) throws  Exception{
        Integer uid = useridUtils.queryUseridByOptenid(request);
        if(uid==null){
            return  new Result("200","failed",null,null);
        }else{
            return new Result("200","success",null,null);
        }
    }



}